package com.changjie.wang.index;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;

import java.io.IOException;

public class EsClient_CreateIndex {
    /**
     * 创建索引数据
     * @param args
     */
    public static void main(String[] args) {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        try {
            //创建索引
            CreateIndexRequest request = new CreateIndexRequest("user");
            CreateIndexResponse response =
                esClient.indices().create(request, RequestOptions.DEFAULT);

            //返回响应状态
            boolean acknowledged = response.isAcknowledged();
            System.out.println("索引创建 \t "+acknowledged);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭ES客户端
            try {
                esClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
